﻿using System.Collections.Generic;
using TicketReservationSystem.Common.Entities;
using TicketReservationSystem.Common.Interfaces;
using TicketReservationSystem.Common.Utils;

namespace TicketReservationSystem.FlightManagement
{
    public class FlightManagement : IFlightManagement
    {
        private readonly IDataAccess data;

        public FlightManagement(IDataAccess data)
        {
            this.data = data;
        }

        public Result Add(City city)
        {
            if (data.Exists(city)) return Result.AlreadyExists;

            data.Add(city);

            return Result.Ok;
        }

        public Result Add(Airport airport)
        {
            if (data.Exists(airport)) return Result.AlreadyExists;

            data.Add(airport);

            return Result.Ok;
        }

        public Result Add(Airline airline)
        {
            if (data.Exists(airline)) return Result.AlreadyExists;

            data.Add(airline);

            return Result.Ok;
        }

        public Result Add(Plane plane)
        {
            if (data.Exists(plane)) return Result.AlreadyExists;

            data.Add(plane);

            return Result.Ok;
        }

        public Result Add(Flight flight)
        {
            if (data.Exists(flight)) return Result.AlreadyExists;

            data.Add(flight);

            return Result.Ok;
        }

        public Result Remove(City city)
        {
            if (!data.Exists(city)) return Result.DoesNotExists;

            var airports = data.AllAirportsInCity(city);

            foreach (var airport in airports)
            {
                data.RemoveAllRelatedFlights(airport);
                data.Remove(airport);
            }

            data.Remove(city);

            return Result.Ok;
        }

        public Result Remove(Airport airport)
        {
            if (!data.Exists(airport)) return Result.DoesNotExists;

            data.RemoveAllRelatedFlights(airport);
            data.Remove(airport);

            return Result.Ok;
        }

        public Result Remove(Airline airline)
        {
            if (!data.Exists(airline)) return Result.DoesNotExists;

            var planes = data.AllPlanesInAirline(airline);

            foreach (var plane in planes)
            {
                data.RemoveAllRelatedFlights(plane);
                data.Remove(plane);
            }

            data.Remove(airline);

            return Result.Ok;
        }

        public Result Remove(Plane plane)
        {
            if (!data.Exists(plane)) return Result.DoesNotExists;

            data.RemoveAllRelatedFlights(plane);
            data.Remove(plane);

            return Result.Ok;
        }

        public Result Remove(Flight flight)
        {
            if (!data.Exists(flight)) return Result.DoesNotExists;

            data.RemoveAllRelatedReservations(flight);
            data.Remove(flight);

            return Result.Ok;
        }

        public Result Remove(Customer customer)
        {
            if (!data.Exists(customer)) return Result.DoesNotExists;

            data.RemoveAllRelatedReservations(customer);
            data.Remove(customer);

            return Result.Ok;
        }

        public Result Remove(Reservation reservation)
        {
            if (!data.Exists(reservation)) return Result.DoesNotExists;

            data.Remove(reservation);

            return Result.Ok;
        }

        public IList<City> AllCities()
        {
            return data.AllCities();
        }

        public IList<Airport> AllAirports()
        {
            return data.AllAirports();
        }

        public IList<Airline> AllAirlines()
        {
            return data.AllAirlines();
        }

        public IList<Plane> AllPlanes()
        {
            return data.AllPlanes();
        }

        public IList<Flight> AllFlights()
        {
            return data.AllFlights();
        }

        public IList<Customer> AllCustomers()
        {
            return data.AllCustomers();
        }

        public IList<Reservation> AllReservations()
        {
            return data.AllReservations();
        }
    }
}
